#! /usr/bin/python
ident = '$Id: contacts.py 116 2006-06-16 17:02:14Z trevor $'
'''Defines the base rex objects class. All rex objects are derived from this class to
standerdize the xml serialization process.
Child classes define their own data dictionary with the properties they are interested in.
'''
import accounts, common, base
from   	xml.sax 	import saxutils
from 	xml.dom		import minidom


################################
class contact( base.object ):
	'''Represents the rex contact objects'''
	def __init__( self, account, xmlstring = None ):
		self.module = 'base'
		self.data = []
		self.data.append( {'name':'id',			'value':'', 'label':'ID', 		'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'uuid',		'value':'', 'label':'UUID', 	'type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'xid',		'value':'', 'label':'XID',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'first_name',	'value':'', 'label':'First Name','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'last_name',	'value':'', 'label':'Last Name','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'email',		'value':'', 'label':'Email',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'html',		'value':'', 'label':'HTML',		'type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,	'options':[ ('1','HTML'), ('0','TEXT') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'created_on',	'value':'', 'label':'Created On','type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'trashed_on',	'value':'', 'label':'Trashed On','type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'memberships',	'value':'', 'label':'Groups',	'type':base.ROT_MEMBERSHIP,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'subscriptions','value':'', 'label':'Subscribed',	'type':base.ROT_SUBS,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'blocks',		'value':'', 'label':'Blocked',	'type':base.ROT_BLOCKS,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'instances',	'value':'', 'label':'Instances',	'type':base.ROT_INS,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.object_name	= "contact"
		self.list_index		= -1
		base.object.__init__( self, account, 'base', xmlstring )

	#~ def __setitem__( self, key, value, dirty = True ):
		#~ '''Handle the groups and publication subscriptions.'''
		#~ if key == "groups":
			#~ self.groups = value
		#~ elif key == "subscriptions":
			#~ self.subscriptions = value
		#~ elif key == "blocks":
			#~ self.blocks = value
		#~ elif key == "instances":
			#~ self.instances = value
		#~ else:
			#~ base.object.__setitem__( self, key, value, dirty )

	def __getitem__( self, key ):
		if key=="name":
			return "%s %s" % ( self["first_name"], self["last_name"] )
		return base.object.__getitem__( self, key )

	#~ def toxml( self ):
		#~ '''Returns the XML representation of the contact object.'''
		#~ sdom = " ".join([ "%s=%s" % (aname, saxutils.quoteattr(avalue[0]) ) \
						#~ for (aname, avalue) in self.data.items() if avalue[1] <= self.max_prop_index] )
		#~ vdom = " ".join([ "<custom name=%s value=%s />" % (saxutils.quoteattr(aname), saxutils.quoteattr(avalue[0]) ) \
						#~ for (aname, avalue) in self.data.items() if avalue[1] > self.max_prop_index ] )
		#~ sdom = "<" + self.object_name + " " + sdom + " >" + vdom + "</" + self.object_name + ">"
		#~ return sdom

	#~ def fromxml( self, xmlstring ):
		#~ '''Expected DOM example
		#~ <contact  />'''
		#~ # Call the base method to handle the attributes, then we parse the issues
		#~ base.object.fromxml( self, xmlstring )
		#~ xmlstring = common.uni2str( xmlstring )

		#~ #try:
		#~ sdom 	= minidom.parseString( xmlstring )
		#~ snode	= sdom.firstChild
		#~ for inode in snode.getElementsByTagName("custom"):
			#~ name = inode.attributes["name"].value
			#~ val  = inode.attributes["value"].value
			#~ #print "Adding %s - %s\n" % ( name, val, )
			#~ self.data[name] = [val, len(self.data), name, rex_base.ROT_MEMO, rex_base.READWRITE, 	0 ]

		#~ #except Exception:
		#~ #	print "Failed to load publication issues from xml.", Exception.__str__()

	def create( self ):
		self.data[3]['value'] = 'New'
		self.data[4]['value'] = 'Contact'
		self.data[3]['dirty'] = True
		self.data[4]['dirty'] = True
		self['id'] = self.execute( "create", dom=self.toxml() )
		self.data[3]['dirty'] = False
		self.data[4]['dirty'] = False
		return True

class group( base.object ):
	'''Represents the issue class'''
	def __init__( self, account, xmlstring = None ):
		self.module = 'base'
		self.data = []
		self.data.append( {'name':'id',			'value':'', 'label':'ID', 		'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'name',		'value':'', 'label':'Name',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'description','value':'', 'label':'Description','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'super_block','value':'0', 'label':'Super Block','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'super_group','value':'0', 'label':'Super Group','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'created_on',	'value':'', 'label':'Created On','type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'memberships',	'value':'', 'label':'Contacts',	'type':base.ROT_MEMBERSHIP,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'subscriptions','value':'', 'label':'Subscribed',	'type':base.ROT_SUBS,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'blocks',		'value':'', 'label':'Blocked',	'type':base.ROT_BLOCKS,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'instances',	'value':'', 'label':'Instances','type':base.ROT_INS,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'licenses',	'value':'', 'label':'Licenses','type':base.ROT_NUMBER,'rw':False,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )		

		self.object_name = "group"
		self.list_index		= -1
		base.object.__init__( self, account, 'base', xmlstring )

	#~ def __setitem__( self, key, value, dirty = True ):
		#~ '''Handle the members and publication subscriptions.'''
		#~ if key == "members":
			#~ self.members = value
		#~ elif key == "subscriptions":
			#~ self.subscriptions = value
		#~ elif key == "blocks":
			#~ self.blocks = value
		#~ elif key == "instances":
			#~ self.instances = value
		#~ elif key == "scope":
			#~ return
		#~ elif key == "showas":
			#~ return
		#~ else:
			#~ base.object.__setitem__( self, key, value, dirty )

	def create( self ):
		self.data[1]['value'] = 'New Group'
		self.data[1]['dirty'] = True
		self['id'] = self.execute( "create", dom=self.toxml() )
		self.data[1]['dirty'] = False
		return True
	
################################
class request( base.object ):
	'''Represents the rex request objects'''
	def __init__( self, account, xmlstring = None ):
		self.module = 'base'
		self.data = []
		self.data.append( {'name':'id',				'value':'', 'label':'ID', 			'type':base.ROT_NUMBER,		'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'queue',			'value':'', 'label':'Queue', 		'type':base.ROT_MEMBERSHIP,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'source',			'value':'', 'label':'Source', 		'type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'target',			'value':'', 'label':'Target',		'type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'source_id',		'value':'', 'label':'Source ID',	'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'target_id',		'value':'', 'label':'Target ID',	'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'contact_id',		'value':'', 'label':'Contact ID',	'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'requester',		'value':'', 'label':'Requester',	'type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'requester_id',	'value':'', 'label':'Requester ID',	'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'interface',		'value':'', 'label':'Interface',	'type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'interface_id',	'value':'', 'label':'Interface ID',	'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'status',			'value':'', 'label':'Status',		'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'reply_email',	'value':'', 'label':'Reply Email',	'type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'cc_email',		'value':'', 'label':'CC Email',		'type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bcc_email',		'value':'', 'label':'BCC Email',	'type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'pivs',			'value':'', 'label':'Pivs',			'type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )		
		self.data.append( {'name':'created_on',		'value':'', 'label':'Created on',	'type':base.ROT_DATE,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'done_on',		'value':'', 'label':'Done on',		'type':base.ROT_DATE,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )		
		self.object_name	= "request"
		self.list_index		= -1
		base.object.__init__( self, account, 'base', xmlstring )

	def __getitem__( self, key ):
		return base.object.__getitem__( self, key )
	
################################
class pq_entry( base.object ):
	'''Represents the rex pq row objects'''
	def __init__( self, account, xmlstring = None ):
		self.module = 'base'
		self.data = []
		self.data.append( {'name':'id',				'value':'', 'label':'ID', 			'type':base.ROT_NUMBER,		'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'request_id',		'value':'', 'label':'Source', 		'type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'status',			'value':'', 'label':'Target',		'type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'created_on',		'value':'', 'label':'Source ID',	'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.object_name	= "pq_entry"
		self.list_index		= -1
		base.object.__init__( self, account, 'base', xmlstring )

	def __getitem__( self, key ):
		return base.object.__getitem__( self, key )
	
	
	
